package cn.edu.scnu.controller;

import cn.edu.scnu.entity.Cart;
import cn.edu.scnu.entity.TbMember;
import cn.edu.scnu.service.CartService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class CartController {
    @Resource
    private CartService cartService;
    //务必记得开启redis否则无法登陆
    @RequestMapping("/cart/addcart")
    public String addcart(String flowerid, HttpSession session){
        TbMember member=(TbMember)session.getAttribute("memberLogin");
        if(member!=null){
            cartService.addcart(member.getEmail(),flowerid);
            return "redirect:/index";
        }else{
            return "redirect:/index/toLogin";
        }
    }
    @RequestMapping("/cart/showcart")
    public String showcart(Model model, HttpSession session){
        TbMember member=(TbMember) session.getAttribute("memberLogin");
        if(member!=null){
            List<Cart> carts = cartService.showcart(member.getEmail());
            model.addAttribute("carts",carts);
            return "showcart";
        }else {
            return "redict:/index/toLogin";
        }
    }
    @RequestMapping("/cart/updateCart")
    public String updateCart(Integer cartId,Integer num){
        cartService.updateCart(cartId,num);
        return "redirect:/cart/showcart";
    }  @RequestMapping("/cart/deleteCart")
    public String updateCart(Integer cartId){
        cartService.removeById(cartId);
        return "redirect:/cart/showcart";
    }
}
